import numpy
import png2obj.image
import png2obj.imagedata

class TextureStyle():
    pass

class GridCellStyle(TextureStyle):
    def __init__(self, data):
        pass

    def texCoords(self, vertices, axis):
        uaxis = (axis + 1) % 3
        vaxis = (axis + 2) % 3
        return numpy.vstack((vertices[uaxis, :], -vertices[vaxis, :]))

    def placeholderImageData(self, scale):
        return 255 * numpy.ones((scale * 16, scale * 16, 4))

class ZProjectionStyle(TextureStyle):
    def __init__(self, data):
        self.nx, self.ny = data.shape
        self.n = max(data.shape)
        self.baseImageData = numpy.zeros((self.n, self.n, 4), int)
        maxData = numpy.max(data)
        self.baseImageData[:self.nx, :self.ny, 0] = data * 255.0 / maxData
        self.baseImageData[:self.nx, :self.ny, 1] = data * 255.0 / maxData
        self.baseImageData[:self.nx, :self.ny, 2] = data * 255.0 / maxData
        self.baseImageData[:, :, 3] = 255

    def texCoords(self, vertices, axis):
        u = (vertices[0, :] + 0.5 * self.nx) / self.n
        v = 1.0 - (vertices[1, :] + 0.5 * self.ny) / self.n # flip v
        result = numpy.vstack((u, v))
        return result

    def placeholderImageData(self, scale):
        return png2obj.imagedata.scale(self.baseImageData, scale)
